/*! ******************************************************************************
 *
 * Created on 2021年9月25日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.di.ui.spoon.navigation.design.classification.category;

import cn.hutool.core.util.ObjectUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.LaniusClassExpandItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.pentaho.di.core.plugins.BasePluginType;
import org.yzbdl.lanius.core.plugins.metastore.LdrCategoryEntity;
import org.yzbdl.lanius.core.plugins.service.CategoryService;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.spoon.Spoon;

/**
 * 新增插件分组类
 * <p>创建插件栏目新增窗口
 *
 * @author 姚宝林
 * @version 1.1
 * @since 1.0
 */
public class CategoryAddShell {
    private static Class<?> PKG = Spoon.class;
    private CategoryService categoryService = CategoryService.getInstance();
    private static CategoryAddShell categoryAddShell;

    private CategoryAddShell() {
    }

    public static CategoryAddShell getInstance(){
        if (ObjectUtil.isEmpty(categoryAddShell)){
            categoryAddShell=new CategoryAddShell();
        }
        return categoryAddShell;
    }

    /**
     * @描述 添加栏目下的分组
     * @data 2020/10/28
     * @param parent
     * @param type
     * @return void
     */
    public void createAddGroupShell(Shell parent, Class<? extends BasePluginType> type, LaniusClassExpandItem item) {
        Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.ON_TOP);
        shell.setBounds((parent.getSize().x - 260) / 2, (parent.getSize().y - 100) / 2, 260, 100);
        shell.open();
        shell.setText(BaseMessages.getString(PKG, "Classification.Shell.New"));
        Image image = GUIResource.getInstance().getImageShellTitle();
        shell.setImage(image);
        Text text = new Text(shell, SWT.BORDER | SWT.CENTER);
        text.setBounds(10, 10, 130, 25);
        Button confirmButton = new Button(shell, SWT.PUSH | SWT.CENTER);
        confirmButton.setBounds(150, 10, 82, 25);
        confirmButton.setText(BaseMessages.getString(PKG, "System.Button.OK"));
        confirmButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                categoryService.addGroupItem(text.getText(), type,item.getClassificationEntity().getId());
                shell.close();
            }
        });
    }

    public void createAddGroupShell(Shell parent, String type, Integer classificationEntityId) {
        Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.ON_TOP);
        shell.setBounds((parent.getSize().x - 260) / 2, (parent.getSize().y - 100) / 2, 260, 100);
        shell.open();
        shell.setText(BaseMessages.getString(PKG, "Classification.Shell.New"));
        Image image = GUIResource.getInstance().getImageShellTitle();
        shell.setImage(image);
        Text text = new Text(shell, SWT.BORDER | SWT.CENTER);
        text.setFocus();
        text.addKeyListener(new KeyAdapter() {
            public void keyPressed( KeyEvent e ) {
                    switch ( e.character){
                        case SWT.CR:
                            categoryService.addGroupItem(text.getText(), type,classificationEntityId);
                            shell.close();
                            break;
                        case SWT.ESC:
                            shell.close();
                            break;
                    }

            }
        });

        text.setBounds(10, 10, 130, 25);
        Button confirmButton = new Button(shell, SWT.PUSH | SWT.CENTER);
        confirmButton.setBounds(150, 10, 82, 25);
        confirmButton.setText(BaseMessages.getString(PKG, "System.Button.OK"));
        confirmButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                categoryService.addGroupItem(text.getText(), type,classificationEntityId);
                shell.close();
            }
        });
    }


    public void updateGroupShell(Shell parent, String type, Integer classificationEntityId,Integer categoryId) {
        Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.ON_TOP);
        shell.setBounds((parent.getSize().x - 260) / 2, (parent.getSize().y - 100) / 2, 260, 100);
        shell.open();
        shell.setText(BaseMessages.getString(PKG, "Classification.Shell.New"));
        Image image = GUIResource.getInstance().getImageShellTitle();
        shell.setImage(image);

        Text text = new Text(shell, SWT.BORDER | SWT.CENTER);
        LdrCategoryEntity entity = categoryService.getOne(categoryId);
        text.setText(entity.getName());
        text.setFocus();
        text.addKeyListener(new KeyAdapter() {
            public void keyPressed( KeyEvent e ) {
                switch ( e.character){
                    case SWT.CR:
                        categoryService.updateCategoryName(categoryId,text.getText());
                        shell.close();
                        break;
                    case SWT.ESC:
                        shell.close();
                        break;
                }

            }
        });

        text.setBounds(10, 10, 130, 25);
        Button confirmButton = new Button(shell, SWT.PUSH | SWT.CENTER);
        confirmButton.setBounds(150, 10, 82, 25);
        confirmButton.setText(BaseMessages.getString(PKG, "System.Button.OK"));
        confirmButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                categoryService.updateCategoryName(categoryId,text.getText());
                shell.close();
            }
        });
    }
}
